import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.util.Arrays;
import java.util.List;
import java.util.Collections;
import java.net.URL;
import java.util.ArrayList;

public class OpenCvTest3 {
    public static void main(String[] args) {
        //加载配置
        //URL url = ClassLoader.getSystemResource("lib/opencv_java455.dll");
        //System.load(url.getPath());
        //读入图片

        System.load("C:\\Users\\A80358\\Downloads\\opencv\\build\\java\\x64\\opencv_java349.dll");

        Mat raw = Imgcodecs.imread("C:\\Users\\A80358\\PycharmProjects\\pythonProject\\S0100000\\02b1732699e54ef48481aaa849aeb23d-20191225160543.jpg");
        Mat src = new Mat();
        //灰度化
        Imgproc.cvtColor(raw, src, Imgproc.COLOR_BGR2GRAY);
        //横向
        Mat gray_x = new Mat();
        Imgproc.Sobel(src, gray_x, -1, 1, 0, 3, 1, 0, Core.BORDER_DEFAULT);
        //Imgcodecs.imwrite("C:\\Users\\A80358\\IdeaProjects\\BarcodeDetector\\pic\\1.jpg", gray_x);
        //竖向
        Mat gray_y = new Mat();
        Imgproc.Sobel(src, gray_y, -1, 0, 1, 3, 1, 0, Core.BORDER_DEFAULT);
        //Imgcodecs.imwrite("C:\\Users\\A80358\\IdeaProjects\\BarcodeDetector\\pic\\2.jpg", gray_y);
        //横竖向图像融合
        Mat gray_xy = new Mat();
        Core.subtract(gray_x, gray_y, gray_xy);
        Imgcodecs.imwrite("C:\\Users\\A80358\\IdeaProjects\\BarcodeDetector\\pic\\3.jpg", gray_xy);
        //Mat handleSrc = Imgcodecs.imread("G:\\opencvPhoto\\result\\gray_xy.jpg");

//handle
        Core.convertScaleAbs(gray_xy, gray_xy);
        Imgcodecs.imwrite("C:\\Users\\A80358\\IdeaProjects\\BarcodeDetector\\pic\\4.jpg", gray_xy);
        Mat out = new Mat();

        // 保存二值化后图片

        Imgproc.adaptiveThreshold(gray_xy, out, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 909, -150);
        Imgcodecs.imwrite("C:\\Users\\A80358\\IdeaProjects\\BarcodeDetector\\pic\\5.jpg", out);

        //定义矩阵
        //Mat juzheng = new Mat();
        //Mat shuchu = new Mat();
        Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(21, 7));
        Mat kernel1 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(21, 21));

        Imgproc.morphologyEx(out, out, Imgproc.MORPH_CLOSE, kernel);
        Imgproc.morphologyEx(out, out, Imgproc.MORPH_OPEN, kernel1);
        Imgproc.morphologyEx(out, out, Imgproc.MORPH_OPEN, kernel1);
        Imgcodecs.imwrite("C:\\Users\\A80358\\IdeaProjects\\BarcodeDetector\\pic\\out.jpg", out);

        List<MatOfPoint> contours=new ArrayList<MatOfPoint>();
        Mat hierarchy = new Mat();
        double maxVal = 0;
        int maxValIdx = 0;
        Imgproc.findContours(out, contours, hierarchy, Imgproc.RETR_EXTERNAL , Imgproc.CHAIN_APPROX_SIMPLE);
        for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++)
        {
            double contourArea = Imgproc.contourArea(contours.get(contourIdx));
            if (maxVal < contourArea)
            {
                maxVal = contourArea;
                maxValIdx = contourIdx;

            }
        }
//        MatOfPoint box = contours.get(maxValIdx);
//
//        //MatOfPoint2f boxx = new MatOfPoint2f();
//        //boxx.convertTo(box, CvType.CV_32F);
//
//        List<MatOfPoint2f> rRect=new ArrayList<MatOfPoint2f>();
//        rRect = Imgproc.minAreaRect(box);
//        //System.out.print(Arrays.toString(box));
//    }
}

//灰度化
//        Imgproc.cvtColor(src, src, Imgproc.COLOR_BGR2GRAY);
//        //横向
//        Mat gray_x = new Mat();
//        Imgproc.Sobel(src, gray_x, -1, 1, 0, 3, 1, 0, Core.BORDER_DEFAULT);
//        Imgcodecs.imwrite("G:\\opencvPhoto\\result\\gray_x.jpg", gray_x);
//        //竖向
//        Mat gray_y = new Mat();
//        Imgproc.Sobel(src, gray_y, -1, 0, 1, 3, 1, 0, Core.BORDER_DEFAULT);
//
//        //横竖向图像融合
//        Mat gray_xy = new Mat();
//        Core.subtract(gray_x, gray_y,  gray_xy);
//        Mat dst = new Mat();
//        Imgproc.blur(src, dst, new Size(3, 3), new Point(1, 1), Core.BORDER_DEFAULT);
//        Mat dst = new Mat();
//        //定义新矩阵
//        Imgproc.adaptiveThreshold(src, dst, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 909, -150);
//        Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(21, 7));
//        Mat kernel1 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(21, 21));
//
//        Imgproc.morphologyEx(mat, dst, Imgproc.MORPH_CLOSE, kernel,, iterations=1);
//        Imgproc.morphologyEx(mat, dilate, Imgproc.MORPH_OPEN, kernel2,  iterations=2);
//
//        List<MatOfPoint> contours=new ArrayList<MatOfPoint>();
//        Mat hierarchy = new Mat();
//        Imgproc.findContours(src, contours, hierarchy, Imgproc.RETR_EXTERNAL , Imgproc.CHAIN_APPROX_SIMPLE);
//
////        接着需要对contourArea(contours）排序：
//
//        for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++)
//        {
//            double contourArea = Imgproc.contourArea(contours.get(contourIdx));
//            if (maxVal < contourArea)
//            {
//                maxVal = contourArea;
//                maxValIdx = contourIdx;
//            }
//        }
//
//        rRect = Imgproc.minAreaRect(mop2f);
//
//        Imgcodecs.imwrite("G:\\opencvPhoto\\result\\gray_xy.jpg", gray_xy);






}

